#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

const int target = 15;
const int arrnum = 100;

int BinarySearch(int array[], int left, int right) {
    if (left >= right) 
        return -1;
    int middle = left + (right - left) / 2;
    if (array[middle] < target) 
        return BinarySearch(array, middle + 1, right);
    else if (array[middle] > target)
        return BinarySearch(array, left, middle - 1);
    else
        return middle;
}

int compare(const void* p1,const void* p2) {
    return *(int*)p1 - *(int*)p2;
}

int main() {
    srand(time(0));   
    int array[arrnum];
    for (int i = 0; i < arrnum; i++)
        array[i] = rand() % 100 + 1;
    qsort(array, arrnum, sizeof(array[0]), compare);
    for (int i = 0; i < arrnum / 10; i++) {
        for (int j = 0; j < 10; j++)
            printf("%d ", array[i * 10 + j]);
        printf("\n");
    }
    int pos = BinarySearch(array, 0, arrnum - 1);
    if (pos == -1) 
        printf("没有找到\n");
    else
        printf("%d 在数组中的下标是 %d\n", target, pos);
    return 0;
}